package com.tria.hcu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tria.hcu.bean.Usuario;


import com.tria.hcu.businessLogic.LoginLogic;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private LoginLogic loginLogic = new LoginLogic();   

    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        try {
            String u = request.getParameter("usuario");
            String c = request.getParameter("contrasenia");
            String redirect = "index.jsp";
            Usuario us = new Usuario(u,c,"");

            if(loginLogic.existeUsuario(us)){
                request.getSession().setAttribute("usuario", us);
                redirect="Login.jsp";
            }else{
                request.getSession().setAttribute("error","Usuario o password INCORRECTO");
            }
            response.sendRedirect(redirect);
        } finally { 
            //out.close();
        }
	}

}
